const router = require('koa-router')()

// 生成雪花ID的方法
const createSnow = function (ctx) {
    let snowFlake = ctx.snowFlake;
    let snowId = snowFlake.createSnow(11);
    return snowId;
}

router.prefix('/register')

router.get("/getStudentInfo", async(ctx,next) => {
    let sql1 = `select * from institute 
              join grade on institute.institute_id = grade.institute_id
              join class on grade.grade_id = class.grade_id`;
  let sql2 = "select institute_id, institute_name from institute"
  let res1 = await ctx.db.EXCUTE(sql1, []);
  let res2 = await ctx.db.EXCUTE(sql2, []);
  console.log(res1, "输出res1数据");  

  function generateRelations(rows, index = 0, relations = []) {
    if (index === rows.length) {
      return relations;
    }
  
    const currentRow = rows[index];
    const instituteId = currentRow.institute_id;
    const gradeId = currentRow.grade_id;
    const classId = currentRow.class_id;
  
    let institute = relations.find((r) => r.value === instituteId);
    if (!institute) {
      institute = { value: instituteId, label: currentRow.institute_name, children: [] };
      relations.push(institute);
    }
  
    let grade = institute.children.find((g) => g.value === gradeId);
    if (!grade) {
      grade = { value: gradeId, label: currentRow.grade_name, number: currentRow.grade_num, children: [] };
      institute.children.push(grade);
    }
  
    let classObj = grade.children.find((c) => c.value === classId);
    if (!classObj) {
      classObj = { value: classId, label: currentRow.class_name, number: currentRow.class_number };
      grade.children.push(classObj);
    }
  
    return generateRelations(rows, index + 1, relations);
  }
  console.log(generateRelations(res1), "输出数据");
  let infoData = generateRelations(res1);
  ctx.body = {code: 200, msg: "查询成功", infoData}
})

router.get('/getTeacherInfo', async(ctx, next) => {
    console.log();
    let sql = "select * from institute";
    let res = await ctx.db.EXCUTE(sql, []);
    console.log(res);
    let infoData = res.map(item => {
        return {value: item.institute_id, label: item.institute_name}
    });
    ctx.body = {code: 200, msg: "查询成功", infoData}
})

router.post('/submit', async(ctx, next) => {
    console.log(ctx.request.body);
    let data = ctx.request.body;
    let user_id = createSnow(ctx);
    let sql = 'insert into user (user_id, user_password, user_name, user_auth) values (?, ?, ?, ?)';
    let sql2 = "insert into teacher (user_id, institute_id, teacher_id, signature, teacher_name, user_name, avatar) values (?, ?, ?, ?, ?, ?, ?)"
    let sql3 = "select user_name, teacher_id from teacher";
    let res2 = await ctx.db.EXCUTE(sql3, []);
    if(data.identity == "teacher") {
      console.log("选择的是教师");
      if(res2.findIndex(item => item.teacher_id == data.teacherNumber) != -1) {
        ctx.body = {code: 500, msg: "该工号教师已存在"}
      } else {
        if(res2.findIndex(item => item.user_name == data.username) == -1) {
          let res1 = await ctx.db.EXCUTE(sql, [user_id, data.cryptograph, data.username, 'teacher_per']);
          let res = await ctx.db.EXCUTE(sql2, [user_id, data.institute_id, data.teacherNumber, '', data.name, data.username, ''])
          ctx.body = {code: 200, msg: "注册成功"}
        } else {
          ctx.body = {code: 500, msg: "该用户名已存在"}
        }
      }
    } else if(data.identity == "student") {
      let sql2 = "insert into student (user_id, institute_id, student_id, signature, student_name, class_id, grade_id, user_name, avatar) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
      let sql3 = "select user_name, student_id from student";
      let res2 = await ctx.db.EXCUTE(sql3, []);
      console.log(res2);
      if(res2.findIndex(item => item.student_id == data.studentNumber) == -1) {
        if(res2.findIndex(item => item.user_name == data.username) == -1) {
          let res = await ctx.db.EXCUTE(sql, [user_id, data.cryptograph, data.username, "student_per"]);
          let res1 = await ctx.db.EXCUTE(sql2, [user_id, data.institute_id, data.studentNumber, '', data.name, data.class_id, data.grade_id, data.username, '']);
          ctx.body = {code: 200, msg: "注册成功"};
        } else {
          ctx.body = {code: 500, msg: "用户名已存在"}
        }
      } else {
        ctx.body = {code: 500, msg: "该学号学生已存在"}
      }
    }

})

module.exports = router